package com.flink.hbase.sql2hdfs.udf;

import org.apache.flink.table.functions.ScalarFunction;

/**
 * 薪资等级计算UDF
 * 根据薪资水平划分不同的薪资等级
 */
public class SalaryLevelUDF extends ScalarFunction {
    private static final long serialVersionUID = 1L;
    
    /**
     * 根据薪资计算薪资等级
     * @param salary 薪资
     * @return 薪资等级
     */
    public String eval(Double salary) {
        if (salary == null) {
            return "未知";
        }
        
        if (salary < 0) {
            return "无效";
        }
        
        if (salary < 3000) {
            return "入门级";
        } else if (salary < 5000) {
            return "初级";
        } else if (salary < 8000) {
            return "中级";
        } else if (salary < 12000) {
            return "中高级";
        } else if (salary < 20000) {
            return "高级";
        } else if (salary < 30000) {
            return "专家级";
        } else if (salary < 50000) {
            return "资深专家";
        } else {
            return "顶级专家";
        }
    }
    
    /**
     * 根据薪资和城市计算薪资等级
     * @param salary 薪资
     * @param city 城市
     * @return 薪资等级
     */
    public String eval(Double salary, String city) {
        if (salary == null) {
            return "未知";
        }
        
        if (salary < 0) {
            return "无效";
        }
        
        // 根据城市调整薪资标准
        double adjustedSalary = salary;
        if (city != null) {
            String cityLower = city.toLowerCase();
            if (cityLower.contains("北京") || cityLower.contains("上海") || 
                cityLower.contains("深圳") || cityLower.contains("广州")) {
                // 一线城市，薪资标准降低20%
                adjustedSalary = salary * 0.8;
            } else if (cityLower.contains("杭州") || cityLower.contains("南京") || 
                      cityLower.contains("成都") || cityLower.contains("武汉")) {
                // 新一线城市，薪资标准降低10%
                adjustedSalary = salary * 0.9;
            }
            // 其他城市保持原标准
        }
        
        return eval(adjustedSalary);
    }
    
    /**
     * 计算薪资区间
     * @param salary 薪资
     * @return 薪资区间
     */
    public String evalRange(Double salary) {
        if (salary == null) {
            return "未知";
        }
        
        if (salary < 0) {
            return "无效";
        }
        
        if (salary < 3000) {
            return "3K以下";
        } else if (salary < 5000) {
            return "3K-5K";
        } else if (salary < 8000) {
            return "5K-8K";
        } else if (salary < 12000) {
            return "8K-12K";
        } else if (salary < 20000) {
            return "12K-20K";
        } else if (salary < 30000) {
            return "20K-30K";
        } else if (salary < 50000) {
            return "30K-50K";
        } else {
            return "50K以上";
        }
    }
    
    /**
     * 判断是否为高薪
     * @param salary 薪资
     * @return 是否为高薪
     */
    public Boolean isHighSalary(Double salary) {
        if (salary == null) {
            return false;
        }
        return salary >= 20000;
    }
    
    /**
     * 计算薪资相对于基准的倍数
     * @param salary 薪资
     * @param baseSalary 基准薪资，默认为8000
     * @return 薪资倍数
     */
    public Double evalMultiple(Double salary, Double baseSalary) {
        if (salary == null || baseSalary == null || baseSalary <= 0) {
            return null;
        }
        return Math.round(salary / baseSalary * 100.0) / 100.0;
    }
    
    /**
     * 计算薪资相对于默认基准（8000）的倍数
     * @param salary 薪资
     * @return 薪资倍数
     */
    public Double evalMultiple(Double salary) {
        return evalMultiple(salary, 8000.0);
    }
    
    /**
     * 根据薪资计算税收等级
     * @param salary 薪资
     * @return 税收等级
     */
    public String evalTaxLevel(Double salary) {
        if (salary == null) {
            return "未知";
        }
        
        if (salary < 0) {
            return "无效";
        }
        
        // 按照个人所得税税率计算
        if (salary <= 3000) {
            return "免税";
        } else if (salary <= 12000) {
            return "低税率(3%)";
        } else if (salary <= 25000) {
            return "中税率(10%)";
        } else if (salary <= 35000) {
            return "较高税率(20%)";
        } else if (salary <= 55000) {
            return "高税率(25%)";
        } else if (salary <= 80000) {
            return "很高税率(30%)";
        } else {
            return "最高税率(35%)";
        }
    }
} 